package com.ipet.app.service.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.ipet.app.bean.UserInfo;
import com.ipet.app.common.App;
import com.ipet.app.util.KLog;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;


public class SQLiteHelperOrm extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "ipet_client";
    private static final int DATABASE_VERSION = 1;

    public SQLiteHelperOrm(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public SQLiteHelperOrm() {
        super(App.getContext(), DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, UserInfo.class);
        } catch (SQLException e) {
            KLog.e(SQLiteHelperOrm.class.getSimpleName(), "创建数据库失败", e);
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i2) {
        try {
            // 更新时，不销毁原有数据
            TableUtils.dropTable(connectionSource, UserInfo.class, true);
            onCreate(sqLiteDatabase, connectionSource);
        } catch (SQLException e) {
            KLog.e(SQLiteHelperOrm.class.getSimpleName(), "更新数据库失败", e);
            e.printStackTrace();
        }
    }

    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();
    }

}
